# coding: utf-8

from sqlalchemy import Column, INT, VARCHAR, BOOLEAN, TEXT
from table.database import DataBase


class User(DataBase):
    __tablename__ = 'user'
    __max_username_length__ = 20
    __max_password_length__ = 20

    id = Column(INT, primary_key=True)
    password = Column(VARCHAR(__max_password_length__))
    username = Column(TEXT, default='')
    studentnum = Column(TEXT, default='')
    name = Column(TEXT, default='')
    group = Column(TEXT, default='')
    uid = Column(TEXT, default='')
    verifycode = Column(TEXT, default='')
    role = Column(TEXT, default='member')
    email = Column(TEXT, default='')
    openid = Column(TEXT, default='')
    avatar = Column(TEXT, default='')

    def to_dict(self):
        return {
            'id': self.id,
            'password': self.password,
            'username': self.username,
            'studentnum': self.studentnum,
            'name': self.name,
            'group': self.group,
            'uid': self.uid,
            'verifycode': self.verifycode,
            'role': self.role,
            'email': self.email,
            'openid': self.openid,
            'avatar': self.avatar
        }

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.username

    def __repr__(self):
        return "| {} | {} |".format(self.username, self.password)


class Device(DataBase):
    __tablename__ = 'device'
    id = Column(INT, primary_key=True)
    serial = Column(TEXT, default='')
    devicename = Column(TEXT, default='')
    devicetype = Column(TEXT, default='')
    amount = Column(INT, default=0)
    remain = Column(INT, default=0)
    detail = Column(TEXT, default='')
    notice = Column(TEXT, default='')
    imgurl = Column(TEXT, default='')

    def to_dict(self):
        return {
            'id': self.id,
            'serial': self.serial,
            'devicename': self.devicename,
            'devicetype': self.devicetype,
            'amount': self.amount,
            'remain': self.remain,
            'detail': self.detail,
            'notice': self.notice,
            'imgurl': self.imgurl,
        }


class Borrow(DataBase):
    __tablename__ = 'borrow'
    id = Column(INT, primary_key=True)
    borrowuser = Column(TEXT, default='')
    deviceid = Column(TEXT, default='')
    borrownum = Column(INT, default=1)
    borrowtime = Column(TEXT, default='')
    keepdays = Column(INT, default=1)
    returnuser = Column(TEXT, default='')
    returntime = Column(TEXT, default='')
    isreturn = Column(TEXT, default='false')
    operatetime = Column(TEXT, default='')


    def to_dict(self):
        return {
            'id': self.id,
            'borrowuser': self.borrowuser,
            'deviceid': self.deviceid,
            'borrownum': self.borrownum,
            'borrowtime': self.borrowtime,
            'keepdays': self.keepdays,
            'returnuser': self.returnuser,
            'returntime': self.returntime,
            'isreturn': self.isreturn,
            'operatetime': self.operatetime
        }


class Borrowteam(DataBase):
    __tablename__ = 'borrowteam'
    id = Column(INT, primary_key=True)
    activitycode = Column(TEXT, default='')
    responseuser = Column(TEXT, default='')
    coverUrl = Column(TEXT, default='')
    description = Column(TEXT, default='')
    borrowtime = Column(TEXT, default='')
    expectReturnTime = Column(TEXT, default=1)
    note = Column(TEXT, default='')
    returntime = Column(TEXT, default='')
    isreturn = Column(TEXT, default='false')
    operatetime = Column(TEXT, default='')
    creator=Column(TEXT, default='')
    def to_dict(self):
        return {
            'id': self.id,
            'activitycode': self.activitycode,
            'responseuser': self.responseuser,
            'borrowtime': self.borrowtime,
            'description': self.description,
            'coverUrl':self.coverUrl,
            'expectReturnTime': self.expectReturnTime,
            'note': self.note,
            'returntime': self.returntime,
            'isreturn': self.isreturn,
            'operatetime': self.operatetime,
            'creator':self.creator
        }

class Projectline(DataBase):
    __tablename__ = 'projectline'
    id = Column(INT, primary_key=True)
    projectname = Column(TEXT, default='')
    pointid = Column(INT, default='')
    pointemo = Column(INT, default=0)
    pointname = Column(TEXT, default='')
    pointtime = Column(TEXT, default='')


class Bilidata(DataBase):
    __tablename__ = 'bilidata'
    id = Column(INT, primary_key=True)
    time = Column(TEXT, default='')
    follow = Column(INT, default='')
    likenum = Column(INT, default='')


class Mqtt(DataBase):
    __tablename__ = 'mqtt'
    id = Column(INT, primary_key=True)
    name = Column(TEXT, default='')
    value = Column(TEXT, default='')


class Data120(DataBase):
    __tablename__ = 'data120'
    id = Column(TEXT, primary_key=True)
    title = Column(TEXT, default='')
    detail = Column(TEXT, default='')
    classify = Column(TEXT, default='')
    author = Column(TEXT, default='')
    like = Column(INT, default=0)
    unlike = Column(INT, default=0)
    likeauthor = Column(TEXT, default='')
    unlikeauthor = Column(TEXT, default='')


class Arinseu(DataBase):
    __tablename__ = 'arinseu'
    id = Column(INT, primary_key=True)
    download = Column(INT, default = 0)
    self = Column(INT, default = 0)
